using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Telerik.Web.UI;

public partial class RadGroupedGridForm : System.Web.UI.Page
{
    protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        var fileName = "sample.xml";

        var file = Server.MapPath(fileName);

        DataSet dataSet = new DataSet();
        dataSet.ReadXml(file);

        //RadGrid1.DataSource = medstarDocuments;
        this.RadGrid1.DataMember = "Rows";
        this.RadGrid1.DataSource = dataSet;
        //this.RadGrid1.DataBind();
    }

    DataSet GetRawData()
    {
        var fileName = "sample.xml";

        var file = Server.MapPath(fileName);

        DataSet dataSet = new DataSet();

        dataSet.ReadXml(file);

        return dataSet;
    }

    DataSet FlattenData(DataSet dataSet, List<string> groupByColumns)
    {
        var temp = dataSet.Copy();

        foreach (var groupByColumn in groupByColumns)
        {
            var rows = new List<DataRow>();

            foreach (DataRow row in temp.Tables[0].Rows)
            {
                rows.Add(row);
            }

            foreach (DataRow row in rows)
            {
                if (row.IsNull(groupByColumn))
                {
                    //temp.Tables[0].LoadDataRow(row.ItemArray, true);
                }
                else
                {
                    var multiValues = ParseLookupFieldValue(row[groupByColumn]);

                    if (multiValues.Count < 2)
                    {
                        //temp.Tables[0].LoadDataRow(row.ItemArray, true);
                    }
                    else
                    {
                        foreach (var value in multiValues)
                        {
                            var newRow = temp.Tables[0].LoadDataRow(row.ItemArray, true);
                            newRow[groupByColumn] = value;
                            newRow.AcceptChanges();
                        }
                        row.Delete();
                    }
                }
            }
        }
        return temp;
    }

    protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
    {
        var ds = GetRawData();

        var exp = e.Expression;

        var multiValueGroupColumns = new List<string>();

        for (int i = 0; i < e.Expression.GroupByFields.Count; i++)
        {
            string groupField = e.Expression.GroupByFields[i].FieldName;

            if (IsMultiValue(ds, groupField))
            {
                multiValueGroupColumns.Add(groupField);
            }
        }

        ds = FlattenData(ds, multiValueGroupColumns);

        var fileName = "sample2.xml";

        //var file = Server.MapPath(fileName);

        //DataSet dataSet = new DataSet();
        //dataSet.ReadXml(file);

        //RadGrid1.DataSource = medstarDocuments;
        this.RadGrid1.DataMember = "Rows";

        this.RadGrid1.DataSource = ds;// dataSet;

        this.RadGrid1.DataBind();

    }

    private bool IsMultiValue(DataSet ds, string groupByColumn)
    {
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            if (row.IsNull(groupByColumn))
            {
                //temp.Tables[0].LoadDataRow(row.ItemArray, true);
            }
            else
            {
                var multiValues = row[groupByColumn].ToString();

                if (multiValues.Contains(";#")) return true;
            }
        }

        return false;
    }

    internal List<string> ParseLookupFieldValue(object input)
    {
        var list = new List<string>();

        try
        {
            var rawValue = input.ToString();

            var values = new SPFieldLookupValueCollection(rawValue);

            if (values.Count == 0)
            {
                var values1 = new SPFieldMultiChoiceValue(rawValue);

                for (int i = 0; i < values1.Count; i++)
                {
                    list.Add(values1[i]);
                }
            }
            else
            {
                foreach (var value in values)
                {
                    list.Add(value.LookupValue);
                }
            }
        }
        catch (Exception ex)
        {
        }

        return list;
    }
}
